package cn.david.org.service.impl;
import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.basic.util.PageList;
import cn.david.org.domain.Department;
import cn.david.org.mapper.DepartmentMapper;
import cn.david.org.query.DepartmentQuery;
import cn.david.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);
        if (department.getParent()==null ){

            department.setDirPath("/"+department.getId());
        }else {
            Department pid = departmentMapper.loadById(department.getParent().getId());
            department.setDirPath(pid.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);

    }

    @Override
    @Transactional
    public void upadte(Department department) {
        if (department.getParent()==null || department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            Department pid = departmentMapper.loadById(department.getParent().getId());
            department.setDirPath(pid.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);

    }


    @Override
    public PageList<Department> queryData(DepartmentQuery query) {
        Long total = departmentMapper.queryCount(query);
        if (total==null || total.intValue()<1){
            return new PageList<>();
        }
        List<Department> list = departmentMapper.queryData(query);
        System.out.println(total+"+++++++++");
        System.out.println(list+"----------");
        return new PageList<>(total,list);

    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        departmentMapper.deleteByIds(ids);

    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }


}
